package com.ruoyi.web.web.form;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.ruoyi.accountant.exception.ValidateException;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/***
 * 查询现金流
 */
@Data
public class CashFlowForm implements Validatable {
    private String time; //表单传递
    private Long ofCompany;
    private Date[] dates;

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @JsonFormat(pattern = "yyyy-MM-dd")
    private Date start; // 开始日期

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @JsonFormat(pattern = "yyyy-MM-dd")
    private Date end; // 结束日期

    public Validation validate() {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy");

        try {
            // 日期 月初到今天
            if(StringUtils.isEmpty(time)) {
                Calendar c = Calendar.getInstance();
                c.setTime(new Date());
                c.add(Calendar.MONTH, -1);

                start = c.getTime();
                end = new Date();
            } else {
                String[] split = time.split("-");
                start = simpleDateFormat.parse(split[0]);
                end = simpleDateFormat.parse(split[1]);
            }
        } catch (ParseException e) {
            throw new ValidateException(e);
        }

        return new Validation(true, "OK");
    }
}
